package mortal.common.preLoadPage
{
	import com.gengine.utils.*;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.*;
	import mortal.common.global.*;
	import mortal.common.component.*;
	
	public class GameLoadBar extends Sprite
	{
		public var startTime:int;
		public var nowTime:int;
		private var _timerText:TextField;
		public var gameRoot:Stage;
		private var _bg:Loader;
		private var _middlePart:Sprite;
		private var logo:Bitmap;
		private var _barTotal:MovieClip;
		private var _barPart:MovieClip;
		private var _totalText:TextField;
		private var _partText:TextField;
		private var _tips:TextField;
		private var _tipsTimer:Timer;
		public var isGameLoadComplete:Boolean = false;
		private static var _instance:GameLoadBar = new GameLoadBar;
		public static var resourceLoader:Loader;
		
		public function GameLoadBar()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
		}
		
		private function onAddedToStage(event:Event) : void
		{
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			this.gameRoot.addEventListener(Event.RESIZE, stageResizeHandler);
			this.initLoaderBar();
		}
		
		private function stageResizeHandler(event:Event = null) : void
		{
			if (this._middlePart)
			{
				this._middlePart.x = this.gameRoot.stageWidth / 2 - 500;
				this._middlePart.y = this.gameRoot.stageHeight / 2 - 290;
			}
			
			if (this._bg && this._bg.content)
			{
				this._bg.x = (this.gameRoot.stageWidth - this._bg.content.width) / 2;
				this._bg.y = (this.gameRoot.stageHeight - this._bg.content.height) / 2 - 40;
			}
		}
		
		private function initLoaderBar() : void
		{
			this._bg = new Loader();
			this._bg.contentLoaderInfo.addEventListener(Event.COMPLETE, this.bgCompleteHandler);
			var url:* = PathConst.mainPath + "assets/modules/PreloaderPage/preLoadPageBg.jpg?v=" + ParamsConst.instance.flashVersion;
			this._bg.load(new URLRequest(url));
			this.addChildAt(this._bg, 0);
			this._middlePart = new Sprite();
			this.addChild(this._middlePart);
			this._barTotal = this.getPreLoadBarMc("LoadBar");
			this._barTotal.x = 180;
			this._barTotal.y = 492;
			this._barTotal.gotoAndStop(0);
			this._middlePart.addChild(this._barTotal);
			this._totalText = new TextField();
			this._totalText.x = this._barTotal.x;
			this._totalText.y = this._barTotal.y + 24;
			this._totalText.width = 630;
			this._totalText.height = 20;
			this._totalText.textColor = 5818367;
			this._totalText.filters = [FilterConst.glowFilter];
			this._totalText.autoSize = TextFieldAutoSize.RIGHT;
			this._totalText.text = "总进度：";
			this._middlePart.addChild(this._totalText);
			this._tips = new TextField();
			this._tips.filters = [FilterConst.glowFilter];
			this._tips.x = this._barTotal.x;
			this._tips.y = this._barTotal.y - 12;
			this._tips.width = 662;
			this._tips.height = 20;
			this._tips.textColor = 16777215;
			this._tips.autoSize = TextFieldAutoSize.CENTER;
			this.changeTipsText();
			this._middlePart.addChild(this._tips);
			this._timerText = new TextField();
			this._timerText.x = this._totalText.x + 140;
			this._timerText.y = this._totalText.y;
			this._timerText.textColor = 16777215;
			this._timerText.filters = [FilterConst.glowFilter];
			this._timerText.autoSize = TextFieldAutoSize.LEFT;
			this._timerText.text = "";
			this._barPart = this.getPreLoadBarMc("LoadBar");
			this._barPart.y = this._barTotal.y + 31;
			this._barPart.x = this._barTotal.x;
			this._barPart.gotoAndStop(0);
			this._middlePart.addChild(this._barPart);
			this._partText = new TextField();
			this._partText.x = this._barPart.x;
			this._partText.y = this._barPart.y + 24;
			this._partText.width = 630;
			this._partText.height = 20;
			this._partText.textColor = 5818367;
			this._partText.filters = [FilterConst.glowFilter];
			this._partText.autoSize = TextFieldAutoSize.RIGHT;
			this._partText.text = "当前进度：";
			this._middlePart.addChild(this._partText);
			var text2:* = new TextField();
			text2.filters = [FilterConst.glowFilter];
			text2.textColor = 5570304;
			text2.x = this._barTotal.x;
			text2.y = 568;
			text2.width = 662;
			text2.height = 20;
			text2.selectable = false;
			text2.autoSize = TextFieldAutoSize.CENTER;
			text2.htmlText = "<font color=\'#FFFFFF\'>如果您是第一次打开游戏，加载文件可能比较大，请耐心等待，</font><font color=\'#54ff00\'><a href=\"event:\"><u>若加载不成功，请点此刷新。</u></a></font>";
			this._middlePart.addChild(text2);
			text2.addEventListener(
				TextEvent.LINK,
				function (event:Event) : void
				{
					BrowerUtil.reload();
				}
			);
			GametipsConfig.instance.load(new URLRequest(PathConst.mainPath + ParamsConst.instance.gameTipsPath + "?v=" + ParamsConst.instance.flashVersion), this.onTipsLoaded);
			this.stageResizeHandler();
		}
		
		private function bgCompleteHandler(event:Event) : void
		{
			this._bg.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.bgCompleteHandler);
			this.stageResizeHandler();
		}
		
		private function onTipsLoaded() : void
		{
			this.startTips();
			this.changeTipsText();
		}
		
		public function startTips() : void
		{
			if (!this._tipsTimer)
			{
				this._tipsTimer = new Timer(GametipsConfig.instance.intervalTime);
				this._tipsTimer.addEventListener(TimerEvent.TIMER, this.onTipsTimerEvent);
				this._tipsTimer.start();
			}
		}
		
		private function onTipsTimerEvent(event:TimerEvent) : void
		{
			this.changeTipsText();
		}
		
		private function changeTipsText() : void
		{
			var info:String = null;
			if (this._tips)
			{
				info = GametipsConfig.instance.getRandomTip();
				if (info != "")
				{
					this._tips.text = "小提示：" + info;
				}
			}
		}
		
		public function show() : void
		{
			if (this.gameRoot)
			{
				this.gameRoot.addChildAt(this, 0);
			}
		}
		
		public function hide() : void
		{
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
			this.dispose();
		}
		
		public function getBitmapData(classname:String) : BitmapData
		{
			var CLASS:Class = null;
			if (resourceLoader)
			{
				CLASS = resourceLoader.contentLoaderInfo.applicationDomain.getDefinition(classname) as Class;
				return new CLASS(0, 0) as BitmapData;
			}
			return new BitmapData(0, 0);
		}
		
		public function getPreLoadBarMc(classname:String) : MovieClip
		{
			var CLASS:Class = null;
			if (resourceLoader)
			{
				CLASS = resourceLoader.contentLoaderInfo.applicationDomain.getDefinition(classname) as Class;
				return new CLASS as MovieClip;
			}
			return new MovieClip();
		}
		
		public function setProgress(type:int, loaded:Number, total:Number, txt:String = "") : void
		{
			var length:* = PreloaderConfig.config[type]["length"];
			var baseValue:* = PreloaderConfig.config[type]["baseValue"];
			var info:* = PreloaderConfig.config[type]["text"];
			var per:* = Math.round(baseValue + loaded / total * length);
			if (this._barTotal)
			{
				this._barTotal.gotoAndStop(per);
				this._totalText.text = "总进度：" + per + "%";
				if (!this.startTime)
				{
					this.startTime = getTimer();
				}
				else
				{
					this.nowTime = getTimer();
					this._timerText.text = "用时： " + (this.nowTime - this.startTime) / 1000 + "秒";
				}
			}
			if (this._barPart)
			{
				per = Math.round(100 * loaded / total);
				this._barPart.gotoAndStop(per);
				this._partText.text = info + "：" + per + "%";
			}
		}
		
		public function dispose() : void
		{
			if (this._tipsTimer)
			{
				this._tipsTimer.stop();
				this._tipsTimer = null;
			}
			_instance = null;
			resourceLoader.unload();
			resourceLoader = null;
		}
		
		public static function get instance() : GameLoadBar
		{
			return _instance;
		}
	}
}
